package com.xjtu.mybatisplus.service.impl;


import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xjtu.mybatisplus.common.constant.RedisConstant;
import com.xjtu.mybatisplus.model.domain.Comment;
import com.xjtu.mybatisplus.service.CommentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
@Slf4j
public class MessageConsumerService {

    @Value("${intensification.redis.host}")
    private String host;

    @Value("${intensification.redis.password}")
    private String password;

    @Value("${intensification.redis.port}")
    private Integer port;

    @Autowired(required = false)
    private CommentService commentService;



    public MessageConsumerService() {
        new Thread(() -> {
            log.info("准备开启新线程");
            Jedis jedis = new Jedis(host, port);
            jedis.auth(password);
            log.info("独立线程,redis连接成功{}",host);
            while (true) {
                List<String> commentsString = jedis.brpop(0, RedisConstant.COMMENT_QUEUE);
                String commentString = commentsString.get(1);
                log.info("获取到的字符串数据为{}", commentString);
                Comment comment = convertJsonToComment(commentString);
                commentService.save(comment);
                //log.info("获取到的评论论为{}", comments);
            }
        }).start();
    }

    public Comment convertJsonToComment(String jsonString) {
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            JsonNode jsonNode = objectMapper.readTree(jsonString);

            // 提取JSON字段
            JsonNode objectFields = jsonNode.get(1);

            Long id = objectFields.has("id") ? objectFields.get("id").asLong() : null;
            Long userId = objectFields.has("user_id") ? objectFields.get("user_id").asLong() : 0;
            Long articleId = objectFields.has("article_id") ? objectFields.get("article_id").asLong() : 0;
            String content = objectFields.has("content") ? objectFields.get("content").asText() : null;
            Long fatherId = objectFields.has("father_id") ? objectFields.get("father_id").asLong() : 0;
            Long commentId = objectFields.has("comment_id") ? objectFields.get("comment_id").asLong() : 0;
            Long replyCommentId = objectFields.has("reply_comment_id") ? objectFields.get("reply_comment_id").asLong() : 0;
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            LocalDateTime createTime = objectFields.has("create_time") ?
                    LocalDateTime.parse(objectFields.get("create_time").asText(), formatter) : null;
            Integer isDeleted = objectFields.has("is_deleted") ? objectFields.get("is_deleted").asInt() : 0;

            Comment comment = new Comment()
                    .setId(id)
                    .setUserId(userId)
                    .setArticleId(articleId)
                    .setContent(content)
                    .setFatherId(fatherId)
                    .setCommentId(commentId)
                    .setReplyCommentId(replyCommentId)
                    .setCreateTime(createTime)
                    .setIsDeleted(isDeleted);
            return comment;

        } catch (Exception e) {
            throw new RuntimeException("Error converting JSON to Comment", e);
        }
    }
}
